package team.seekers.hula.course.resource.sectionContent.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.course.resource.problem.entity.dto.ProblemDTO;
import team.seekers.hula.course.resource.problem.entity.vo.ProblemVO;
import team.seekers.hula.course.resource.problem.service.ProblemService;
import team.seekers.hula.course.resource.sectionContent.controller.vo.SectionBindContextDTO;
import team.seekers.hula.course.resource.sectionContent.dao.SectionContentDao;
import team.seekers.hula.course.resource.sectionContent.entity.dao.SectionContentDO;
import team.seekers.hula.course.resource.sectionContent.entity.dto.*;
import team.seekers.hula.course.resource.sectionContent.entity.vo.SectionContentVO;
import team.seekers.hula.course.structure.section.entity.dto.SectionDTO;
import team.seekers.hula.system.resource.dao.VideoDao;
import team.seekers.hula.system.resource.entity.dto.VideoDTO;
import team.seekers.hula.system.resource.service.VideoService;

import java.util.*;

/**
 * date: 2022/5/11 10:55
 *
 * @author GH
 * QQ 1124813753
 */
@Service
public class SectionContentManageService {

    @Autowired
    private SectionContentDao sectionContentDao;

    @Autowired
    private ProblemService problemService;

    @Autowired
    private VideoService videoService;


    @Transactional(rollbackFor = Exception.class)
    public void save(SectionContentDTO contentDTO, List<String> contentIds) {
        sectionContentDao.saveOrUpdate(SectionContentDO.fromDTO(contentDTO));
    }

    public String update(SectionContentUpdateDTO updateDTO) {
        SectionContentDO sectionContentDO = sectionContentDao.lambdaQuery().eq(SectionContentDO::getId, updateDTO.getId()).one();

        // 修改原课时为不可见
        sectionContentDao.lambdaUpdate()
                .set(SectionContentDO::getIsVisible,0)
                .eq(SectionContentDO::getId,sectionContentDO.getId())
                .update();

        BeanUtils.copyProperties(updateDTO,sectionContentDO);
        sectionContentDO.setId(null);
        sectionContentDO.setVersion(sectionContentDO.getVersion()+1);
        sectionContentDao.save(sectionContentDO);

        return sectionContentDO.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String sectionId, SectionDTO sectionDTO) {
        sectionContentDao.removeById(sectionId);
    }

    public List<SectionContentListDTO> querySectionContentsBySectionId(String sectionId) {
        List<SectionContentDO> contentDOList = sectionContentDao.lambdaQuery().select(
                SectionContentDO::getId,
                SectionContentDO::getSectionId
        ).eq(SectionContentDO::getSectionId, sectionId).list();

        List<SectionContentListDTO> contentListDTOS = SectionContentDO.toVOList(contentDOList);
        return contentListDTOS;
    }

    public void bindSectionContext(List<SectionBindContextDTO> bindContextDTO) {

    }

    public List<SectionContentDTO> querySectionContentDTOsBySectionId(String sectionId) {
        List<SectionContentDO> contentDOList = sectionContentDao.lambdaQuery()
                .eq(SectionContentDO::getSectionId, sectionId)
                .eq(SectionContentDO::getIsVisible, 1)
                .orderByAsc(SectionContentDO::getSequence)
                .list();
        return SectionContentDO.toDTOList(contentDOList);
    }

    public List<SectionContentVO> queryBySectionId(String sectionId) {
        List<SectionContentDO> list = sectionContentDao.lambdaQuery()
                .eq(SectionContentDO::getSectionId, sectionId)
                .eq(SectionContentDO::getIsVisible, 1)
                .orderByAsc(SectionContentDO::getSequence)
                .list();

        ArrayList<SectionContentVO> sectionContentVOS = new ArrayList<>();
        for (SectionContentDO sectionContentDO : list) {
            SectionContentVO sectionContentVO = new SectionContentVO();
            BeanUtils.copyProperties(sectionContentDO,sectionContentVO);
            if(sectionContentDO.getType() == 1){
                // 是视频
                VideoDTO videoDTO = videoService.queryInfoById(sectionContentDO.getContent());
                sectionContentVO.setVideo(videoDTO);
            }
            if(sectionContentDO.getType() == 2){
                // 是题目
                ProblemVO infoVO = problemService.getInfoVO(sectionContentDO.getContent());
                sectionContentVO.setProblem(infoVO);
            }
            sectionContentVOS.add(sectionContentVO);
        }
        return sectionContentVOS;
    }

//    @Transactional
//    public void bindSectionContext(List<SectionBindContextDTO> bindContextDTO) {
//        // 先清除所有绑定记录
//        ArrayList<Long> sectionIds = new ArrayList<>();
//        for (SectionBindContextDTO sectionBindContextDTO : bindContextDTO) {
//            sectionIds.add(sectionBindContextDTO.getSectionId());
//        }
//        sectionContentDao.lambdaUpdate().in(SectionContentDO::getSectionId,sectionIds).remove();
//
//
//        int sequence = 0;
//        // 重新完成绑定
//        ArrayList<SectionContentDO> sectionContentDOS = new ArrayList<>();
//        for (SectionBindContextDTO sectionBindContextDTO : bindContextDTO) {
//            List<SectionContentBindDTO> sectionContents = sectionBindContextDTO.getSectionContents();
//            if(sectionContents==null)
//                continue;
//            sequence = 0;
//            for (SectionContentBindDTO sectionContentBindDTO : sectionContents) {
//                SectionContentDO sectionContentDO = new SectionContentDO();
//                BeanUtils.copyProperties(sectionContentBindDTO,sectionContentDO);
//                sectionContentDO.setSectionId(sectionBindContextDTO.getSectionId());
//                sectionContentDO.setSequence(sequence);
//                sequence++;
//                if(sectionContentBindDTO.getProblems() != null && sectionContentBindDTO.getProblems().size()>0){
//                    HashMap<String, String> problems = new HashMap<>();
//                    for (SectionContentBindProblemDTO problem : sectionContentBindDTO.getProblems()) {
//                        problems.put(problem.getProgress().toString(),problem.getProblemId().toString());
//                    }
//                    sectionContentDO.setProblems(BaseConvertUtils.mapToString(problems));
//                }
//                sectionContentDOS.add(sectionContentDO);
//            }
//        }
//        sectionContentDao.saveBatch(sectionContentDOS);
//    }
//
//
//    public List<Long> getIdsBySectionIds(List<Long> sectionIds) {
//        List<SectionContentDO> list = sectionContentDao.lambdaQuery().select(
//                SectionContentDO::getId
//        ).in(SectionContentDO::getSectionId, sectionIds).list();
//
//        ArrayList<Long> sectionContentIds = new ArrayList<>();
//        for (SectionContentDO sectionContentDO : list) {
//            sectionContentIds.add(sectionContentDO.getId());
//        }
//        return sectionContentIds;
//    }
}
